package com.education.teacher.Interceptor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.MethodParameter;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lzh on 2017/8/7.
 * 日志拦截器 记录请求相关的日志信息
 */
public class LogInterceptor implements HandlerInterceptor {

    private Logger logger= LogManager.getLogger(LogInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String handlerMethodName="";
        if(handler instanceof HandlerMethod){
            HandlerMethod method= (HandlerMethod) handler;
            handlerMethodName=method.getMethod().getName();
        }

        String reqMethodName=request.getMethod();
        logger.info("RequestMapping:  "+request.getRequestURI()+"  RequestMethod: "+reqMethodName+"  params："+
                request.getParameterMap().toString()+"   HandlerMethod: "+handlerMethodName);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
